package hms.payment;

import java.sql.*;
import java.util.Calendar;
import java.util.GregorianCalendar;

import hms.database.*;

/*Using the DBRowObject class, this class passes data from
 * the HMS database to the Credit payment option
 * 
 * @author Edgar Onukwugha*/

public class CreditOption extends DBRowObject implements MoneyOption{

	private int payment_method_ID;
	private String card_number;
	private String CVV2;
	private Date expiration_date;
	private String card_distributor;
	private String bank_name;
	private double credit_amount;
	
	public CreditOption(){
		super();
		payment_method_ID = -1;
		card_number = "xxxxxxxxxxxxxxxx";
		CVV2 = "XXX";
		expiration_date = null;
		card_distributor = "NONE";
		bank_name = "NONE";
		credit_amount = 0;
	}
	
	
	public CreditOption(int payment_ID){
		super(payment_ID);
	}
	
	@Override
	protected String[] getFieldNames() {
		String[] object = {
		"PAYMENT_METHOD_ID",
		"CARD_NUMBER",
		"CVV2",
		"EXPIRATION_DATE",
		"CARD_DISTRIBUTOR",
		"BANK_NAME",
		"CREDIT_AMOUNT"};
		return object;
	}

	/////////////////////////////////////////////////
	/////               SETTERS                //////
	/////////////////////////////////////////////////
	
	public void setPaymentMethodID(int i){
		payment_method_ID = i;
		markAsChanged();
	}
	
	public void setCardNumber(String i){
		card_number = i;
		markAsChanged();
	}
	
	public void setCVV2(String i){
		CVV2 = i;
		markAsChanged();
	}
	
	public void setExpirationDate(Date i){
		expiration_date = i;
		markAsChanged();
	}
	
	public void setCardDistributor(String i){
		card_distributor = i;
		markAsChanged();
	}
	
	public void setBankName(String i){
		bank_name = i;
		markAsChanged();
	}
	
	public void setCreditAmount(Double i) {
		credit_amount = i;
		markAsChanged();
	}
	

	public void setExpirationDate(String month, String year) {
		Calendar c = new GregorianCalendar();
		final String[] months = { "Janurary", "February", "March", "April",
				"May", "June", "July", "August", "September", "October",
				"November", "December" };
		for (int i = 0; i < 12; i++) {
			if (months[i].equals(month)) {
				c.set(Calendar.MONTH, i);
				c.set(Calendar.YEAR, Integer.parseInt(year));
				expiration_date = new Date(c.getTimeInMillis());
				break;
			}
		}

	}
	
	/////////////////////////////////////////////////
	/////               GETTERS                //////
	/////////////////////////////////////////////////
	
	public int getPaymentMethodID(){
		return payment_method_ID;
	}
	
	public String getCardNumber(){
		return card_number;	
	}
	
	public String getCVV2(){
		return CVV2;
	}
	
	public Date getExpirationDate(){
		return expiration_date;
	}
	
	public String getCardDistributor(){
		return card_distributor;
	}
	
	public String getBankName(){
		return bank_name;
	}
	
	public double getCreditAmount() {
		return credit_amount;
	}
	
	@Override
	protected void loadPreparedStatement(PreparedStatement stmt)
			throws SQLException {
		stmt.setInt(1, payment_method_ID);
		stmt.setString(2, card_number);
		stmt.setString(3, CVV2);
		stmt.setDate(4, expiration_date);
		stmt.setString(5, card_distributor);
		stmt.setString(6, bank_name);
		stmt.setDouble(7, credit_amount);
	}

	@Override
	protected String getUniqueIdentifierFieldName() {
		// TODO Auto-generated method stub
		return "PAYMENT_ID";
	}

	@Override
	protected String getTableName() {
		// TODO Auto-generated method stub
		return "CREDIT_CARD";
	}

	@Override
	protected void loadFromDatabase(ResultSet rs) throws SQLException {
		this.payment_method_ID = rs.getInt("PAYMENT_METHOD_ID");
		this.card_number = rs.getString("CARD_NUMBER");
		this.CVV2 = rs.getString("CVV2");
		this.expiration_date = rs.getDate("EXPIRATION_DATE");
		this.card_distributor = rs.getString("CARD_DISTRIBUTOR");
		this.bank_name = rs.getString("BANK_NAME");
		this.credit_amount = rs.getDouble("CREDIT_AMOUNT");
	}

	@Override
	protected boolean allowDuplicateRows() {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public double getAmount() {
		// TODO Auto-generated method stub
		return credit_amount;
	}

}
